home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / lingua13.zip / LINGUA.C < prev    next >
C/C++ Source or Header  |  1994-03-13  |  9KB  |  249 lines

  1. /* --------------------------------------------*\
  2. | lingua.c (version 1.3) -- (C) SichemSoft 1994 |
  3. | Roghorst 160, 6708 KS Wageningen, Netherlands |
  4. | utility for language independent applications |
  5. | author: Anneke Sicherer-Roetman, date: 940311 |
  6. \* --------------------------------------------*/
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "lingua.h"
  11.  
  12. /* opens file with error checking */
  13. static FILE *openfile(char *name,char *mode)
  14. {
  15.    FILE *fp;
  16.  
  17.    fp=fopen(name,mode);
  18.    if (!fp) printf("%s not opened\n",name);
  19.    return fp;
  20. } /* openfile */
  21.  
  22. /* writes array size in text area of file */
  23. unsigned writesize(FILE *fp,unsigned long pos,unsigned size)
  24. {
  25.    unsigned long filpos; unsigned checksum; register int i;
  26.  
  27.    filpos=ftell(fp); fseek(fp,pos,SEEK_SET);
  28.    fwrite(&size,sizeof(size),1,fp);
  29.    fseek(fp,filpos,SEEK_SET);
  30.    for (i=0,checksum=0; i<sizeof(size); i++)
  31.       checksum+=((char*)(&size))[i]^UIT_ENCRYPT;
  32.    return checksum;
  33. }
  34.  
  35. /* returns pointer to string value by skipping spaces and tabs */
  36. char *value(char *buf)
  37. {
  38.    char *p;
  39.    p=strchr(buf,' '); if (!p) p=strchr(buf,'\t');
  40.    if (!p) return NULL;
  41.    *p=0; p++; while (*p==' ' || *p=='\t') p++;
  42.    return p;
  43. }
  44.  
  45. /* main text processing routine */
  46. int main(int argc,char *argv[])
  47. {
  48.    FILE *txt,*etf,*header;
  49.    char fname[81],buf[256],linefeed[3],*p,*q,space='_';
  50.    unsigned long memoffset=0,filoffset=0,offset=0;
  51.    unsigned long filpos1=0,filpos2=0,filpos3=0;
  52.    unsigned memcount=0,filcount=0,lines=0,len=0,l=0;
  53.    unsigned checksum=0,headlen=0,arrsize=0;
  54.    unsigned array=FALSE,multi=FALSE,file=FALSE;
  55.  
  56.    /* copyright message and arguments check */
  57.    puts("Lingua v1.3 - (C)1994 SichemSoft Wageningen Netherlands");
  58.    if (argc!=2 && argc!=3) {
  59.       puts("lingua <file> [<version>]"); return 1;
  60.    }
  61.  
  62.    /* open all files */
  63.    strcpy(fname,argv[1]);
  64.    if (!strchr(fname,'.')) strcat(fname,".txt");
  65.    if ((txt=openfile(fname,"r"))==NULL) return 2;
  66.    if ((header=openfile("ui_text.h","w"))==NULL) return 2;
  67.    strcpy(strchr(fname,'.')+1,"etf");
  68.    if ((etf=openfile(fname,writeRA))==NULL) return 2;
  69.  
  70.    /* start of UI_TEXT.H */
  71.    fputs("/* ui_text.h */\n\n",header);
  72.    fputs("#ifndef UI_TEXT_H\n",header);
  73.    fputs("#define UI_TEXT_H\n\n",header);
  74.    fputs("#ifdef __cplusplus\n",header);
  75.    fputs("extern \"C\" {\n",header);
  76.    fputs("#endif\n\n",header);
  77.    fputs("int ui_loadtext(char *fname,char *vers);\n",header);
  78.    fputs("void ui_unloadtext(void);\n",header);
  79.    fputs("char *ui_filetext(unsigned pos);\n",header);
  80.    fputs("char **ui_filearray(unsigned pos);\n",header);
  81.    fputs("extern char **ui_text;\n\n",header);
  82.  
  83.    /* start of .ETF (filename+version), checksum and counters */
  84.    fprintf(etf,"%s%s\032",fname,argc==3?argv[2]:"");
  85.    headlen=strlen(fname)+strlen(argc==3?argv[2]:"")+1;
  86.    filpos1=ftell(etf);
  87.    fwrite(&checksum,sizeof(checksum),1,etf);
  88.    fwrite(&memcount,sizeof(memcount),1,etf);
  89.    fwrite(&filcount,sizeof(memcount),1,etf);
  90.    fwrite(&memoffset,sizeof(memoffset),1,etf);
  91.  
  92.    /* count lines, determine offsets and write to .ETF and UI_TEXT.H */
  93.    while (fgets(buf,255,txt)) {
  94.       l=strlen(buf)-1; if (buf[l]=='\n') buf[l]='\0';
  95.       lines++;
  96.       if (!buf[0]) continue;
  97.       if (buf[0]=='#') { /* comment */
  98.          if (!strcmp(buf+1,"FILE")) {
  99.             if (file) goto fatal;
  100.             file=TRUE; filpos2=ftell(etf);
  101.          } else if (!strncmp(buf+1,"SPACE",5)) {
  102.             p=value(buf); space=*p; if (!space) goto fatal;
  103.          }
  104.          continue;
  105.       }
  106.       p=value(buf);
  107.       len=strlen(p); if (!strcmp(p,"-")) len=0;
  108.       l=strlen(buf)-1;
  109.       if (buf[l]=='[') { /* array identifier */
  110.          if (l>0) { /* first element */
  111.             buf[l]='\0'; array=TRUE; multi=FALSE;
  112.             if (file) fprintf(header,"#define %-30s (ui_filearray(%u))\n",buf,filcount);
  113.             else      fprintf(header,"#define %-30s (ui_text+%u)\n",buf,memcount);
  114.          } else { /* next element */
  115.             if (!array) goto fatal;
  116.          }
  117.          if (file) {
  118.             fwrite(&filoffset,sizeof(filoffset),1,etf);
  119.             filoffset+=len+1; filcount++;
  120.             if (l>0) filoffset+=sizeof(arrsize);
  121.          } else {
  122.             fwrite(&memoffset,sizeof(memoffset),1,etf);
  123.             memoffset+=len+1; memcount++;
  124.          }
  125.       } else if (buf[l]=='/') { /* multi line text */
  126.          if (l>0) { /* first line */
  127.             buf[l]='\0'; multi=TRUE; array=FALSE;
  128.             if (file) {
  129.                fprintf(header,"#define %-30s ui_filetext(%u)\n",buf,filcount);
  130.                fwrite(&filoffset,sizeof(filoffset),1,etf);
  131.                filoffset+=len+1; filcount++;
  132.             } else {
  133.                fprintf(header,"#define %-30s ui_text[%u]\n",buf,memcount);
  134.                fwrite(&memoffset,sizeof(memoffset),1,etf);
  135.                memoffset+=len+1; memcount++;
  136.             }
  137.          } else { /* next line */
  138.             if (file) filoffset+=len+strlen(lf);
  139.                  else memoffset+=len+strlen(lf);
  140.             if (!multi) goto fatal;
  141.          }
  142.       } else { /* normal identifier */
  143.          array=FALSE; multi=FALSE;
  144.          if (file) {
  145.             fprintf(header,"#define %-30s ui_filetext(%u)\n",buf,filcount);
  146.             fwrite(&filoffset,sizeof(filoffset),1,etf);
  147.             filoffset+=len+1; filcount++;
  148.          } else {
  149.             fprintf(header,"#define %-30s ui_text[%u]\n",buf,memcount);
  150.             fwrite(&memoffset,sizeof(memoffset),1,etf);
  151.             memoffset+=len+1; memcount++;
  152.          }
  153.       }
  154.    }
  155.    if (file) fwrite(&filoffset,sizeof(filoffset),1,etf);
  156.  
  157.    /* encrypt lines and write to .ETF */
  158.    rewind(txt); array=multi=file=FALSE; space='_';
  159.    strcpy(linefeed,lf); for (l=0; linefeed[l]; l++) linefeed[l]^=UIT_ENCRYPT;
  160.    while (fgets(buf,255,txt)) {
  161.       l=strlen(buf)-1; if (buf[l]=='\n') buf[l]='\0';
  162.       if (!buf[0]) continue;
  163.       if (buf[0]=='#') { /* comment */
  164.          if (!strcmp(buf+1,"FILE")) file=TRUE;
  165.          else if (!strncmp(buf+1,"SPACE",5)) {
  166.             p=value(buf); space=*p;
  167.          }
  168.          continue;
  169.       }
  170.       p=value(buf);
  171.       len=strlen(p); if (!strcmp(p,"-")) p[0]='\0';
  172.       for (q=p; *q==space; q++) *q=' ';
  173.       for (q=p+len-1; *q==space; q--) *q=' ';
  174.       l=strlen(buf)-1; len=strlen(p)+1;
  175.       if (buf[l]=='[') { /* array identifier */
  176.          if (file) {
  177.             if (l>0) {
  178.                filpos3=ftell(etf);
  179.                fwrite(&arrsize,sizeof(arrsize),1,etf);
  180.                array=TRUE; arrsize=1;
  181.             } else arrsize++;
  182.          }
  183.          if (multi) putc('\0'^UIT_ENCRYPT,etf);
  184.          multi=FALSE;
  185.       } else if (buf[l]=='/') { /* multi line text */
  186.          if (l==0 && multi) { fputs(linefeed,etf); checksum+=lfchk; }
  187.          if (l>0) {
  188.             if (file && array) {
  189.                checksum+=writesize(etf,filpos3,arrsize);
  190.                array=FALSE;
  191.             }
  192.             if (multi) putc('\0'^UIT_ENCRYPT,etf);
  193.             multi=TRUE;
  194.          }
  195.       } else { /* normal identifier */
  196.          if (file && array) {
  197.             checksum+=writesize(etf,filpos3,arrsize);
  198.             array=FALSE;
  199.          }
  200.          if (multi) putc('\0'^UIT_ENCRYPT,etf);
  201.          multi=FALSE;
  202.       }
  203.       for (q=p; *q; q++) {
  204.          checksum+=(unsigned char)(*q); *q^=UIT_ENCRYPT; putc(*q,etf);
  205.       }
  206.       if (!multi) putc('\0'^UIT_ENCRYPT,etf);
  207.    }
  208.    if (multi) putc('\0'^UIT_ENCRYPT,etf);
  209.    if (file && array) checksum+=writesize(etf,filpos3,arrsize);
  210.  
  211.    /* write checksum and number of items and bytes to .ETF */
  212.    fseek(etf,filpos1,SEEK_SET);
  213.    fwrite(&checksum,sizeof(checksum),1,etf);
  214.    fwrite(&memcount,sizeof(memcount),1,etf);
  215.    fwrite(&filcount,sizeof(memcount),1,etf);
  216.    fwrite(&memoffset,sizeof(memoffset),1,etf);
  217.  
  218.    /* correct file item offsets */
  219.    if (filcount) {
  220.       fseek(etf,filpos2,SEEK_SET);
  221.       offset=headlen+sizeof(checksum)+sizeof(memcount)+sizeof(filcount)+
  222.              sizeof(memoffset)+memcount*sizeof(memoffset)+
  223.              (filcoun